<template>
    <div class="white-box">
        <div class="table-mb">
            <el-button type="primary" @click="addGrade">添加</el-button>
        </div>
        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <el-table-column prop="level_id" label="ID" width="100"></el-table-column>
            <el-table-column prop="level_name" label="等级名称" min-width="180"></el-table-column>
            <el-table-column label="排序" min-width="130">
                <template #default="{ row }">
                    <el-input
                        v-model.trim="row.order_by"
                        clearable
                        placeholder="请输入排序数字"
                        oninput="value=value.replace(/[^\d]/g,'')"
                        style="width: 100px"
                        @change="() => inputSort(row)"
                    />
                </template>
            </el-table-column>
            <el-table-column prop="updated_at_label" label="更新时间" width="180"></el-table-column>
            <el-table-column label="操作" width="240" class-name="operation-nav" fixed="right">
                <template #default="{ row }">
                    <el-button link type="primary" class="theme-btn" @click="clickEdit(row)">编辑 </el-button>
                    <el-popconfirm title="是否确定删除？" confirm-button-text="确定" cancel-button-text="取消" @confirm="clickDel(row)">
                        <template #reference>
                            <el-button link type="primary" class="theme-btn">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>

        <!-- 添加/编辑 -->
        <el-dialog
            v-model="dialogGrade"
            :title="grade_type == 1 ? '添加等级' : '编辑等级'"
            width="560px"
            :append-to-body="true"
            :destroy-on-close="true"
            :close-on-click-modal="false"
            @close="closeGrade"
        >
            <el-form ref="ruleGradeRef" :model="ruleGradeForm" :rules="ruleCheckGrade" label-width="120px">
                <el-form-item label="等级名称" prop="grade_name">
                    <el-input v-model.trim="ruleGradeForm.grade_name" clearable placeholder="请输入等级名称" />
                </el-form-item>
                <el-form-item label="排序" prop="order_by" required>
                    <el-input
                        v-model.number="ruleGradeForm.order_by"
                        clearable
                        oninput="value=value.replace(/[^\d]/g,'')"
                        placeholder="请输入排序数字"
                    />
                </el-form-item>
                <el-form-item>
                    <div class="flexs" style="margin-top: 20px">
                        <el-button type="primary" :loading="form_loading" @click="submitForm(ruleGradeRef)"> 确认 </el-button>
                        <el-button @click="resetForm(ruleGradeRef)">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>

        <go-back />
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, reactive, watch,onBeforeUnmount } from "vue"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

onBeforeUnmount(() => {
    
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, pubParams, getdata } = OneLoad()

// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
    }
    getdata("product/level/index", params)
}

/**
 * 添加/编辑属性值
 * @param {Boolean} dialogGrade 添加/编辑弹窗状态
 * @param {Boolean} form_loading 提交按钮状态
 * @param {Number} grade_type 1是添加，2是编辑
 * @param {Array} ruleGradeRef 表单规则
 * @param {Object} ruleGradeForm  表单字段
 * @param {Object} ruleCheckGrade  表单提示
 * @param {Function} submitForm 提交表单
 * @param {Function} closeGrade 关闭弹窗
 */
const dialogGrade = ref(false)
const grade_type = ref(1)
const form_loading = ref(false)
const ruleGradeRef = ref(null)
const ruleGradeForm = reactive({
    id: "",
    grade_name: "",
    order_by: "100",
})
const ruleCheckGrade = reactive({
    grade_name: [{ required: true, message: "请输入等级名称", trigger: "blur" }],
    order_by: [{ required: true, message: "请输入排序数字", trigger: "blur" }],
})
const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid, fields) => {
        if (valid) {
            form_loading.value = true
            let data = {
                level_name: ruleGradeForm.grade_name,
                order_by: ruleGradeForm.order_by,
            }
            if (grade_type.value == 2) {
                data.level_id = ruleGradeForm.id
            }
            let url = grade_type.value == 1 ? "product/level/add" : "product/level/update"
            proxy.$axios
                .post(url, data)
                .then((res) => {
                    form_loading.value = false
                    if (res.data.code == 0) {
                        dialogGrade.value = false
                        proxy.$message({ message: `${grade_type.value == 1 ? '添加成功！' : '编辑成功！'}`, type: "success" })
                        pubParams(getList)
                    } else {
                        proxy.$message({message:res.data.msg,type:"error"})
                    }
                })
                .catch((err) => {
                    form_loading.value = false
                })
        }
    })
}
const addGrade = () => {
    grade_type.value = 1
    dialogGrade.value = true
}
const clickEdit = (row) => {
    dialogGrade.value = true
    setTimeout(() => {
        grade_type.value = 2
        ruleGradeForm.id = row.level_id
        ruleGradeForm.grade_name = row.level_name
        ruleGradeForm.order_by = row.order_by
    }, 50)
}
const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
const closeGrade = () => {
    dialogGrade.value = false
    resetForm(ruleGradeRef.value)
}
// 删除
function clickDel(row) {
    proxy.$axios
        .post("product/level/delete", {
            level_id: row.level_id,
        })
        .then((res) => {
            if (res.data.code == 0) {
                pubParams(getList)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

// 排序
const inputSort = (row) => {
    if (!row.order_by) {
        proxy.$message({ message: "请输入排序数字", type: "warning" })
        return
    }
    proxy.$axios
        .post("product/level/sort", {
            level_id: row.level_id,
            order_by: row.order_by,
        })
        .then((res) => {
            if (res.data.code == 0) {
                pubParams(getList)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}
</script>
<script>
export default {
    name: "GoodsGrade",
}
</script>

<style lang="less" scoped></style>
